package com.mingye.board;

import com.mingye.constants.Direction;
import com.mingye.components.BoardPanel;
import com.mingye.model.chess.Box;
import com.mingye.model.chess.Food;
import com.mingye.model.chess.Person;
import com.mingye.model.grid.BaseGrid;

import java.util.List;

/**
 * 棋盘基类
 *
 * @author daixinyi
 */
public abstract class Board {

    /**
     * 所属棋盘面板
     */
    protected BoardPanel boardPanel;

    /**
     * 棋盘
     */
    protected BaseGrid[][] boardArr;

    /**
     * 箱子列表
     */
    protected List<Box> boxList;

    /**
     * 食物列表
     */
    protected List<Food> foodList;

    /**
     * 人
     */
    protected Person person;

    /**
     * 完成推送的箱子数
     */
    protected int finishBoxCount;

    public Board() {
        initBoard();
    }

    /**
     * 初始化棋盘
     */
    public void initBoard() {
        initBoardArr();
        initBox();
        initFood();
        initPerson();
    }

    /**
     * 初始化棋盘二维数组
     */
    protected abstract void initBoardArr();

    /**
     * 初始化箱子信息
     */
    protected abstract void initBox();

    /**
     * 初始化食物信息
     */
    protected abstract void initFood();

    /**
     * 初始化人信息
     */
    protected abstract void initPerson();

    public BoardPanel getBoardPanel() {
        return boardPanel;
    }

    public void setBoardPanel(BoardPanel boardPanel) {
        this.boardPanel = boardPanel;
    }

    public BaseGrid[][] getBoardArr() {
        return boardArr;
    }

    public List<Box> getBoxList() {
        return boxList;
    }

    public List<Food> getFoodList() {
        return foodList;
    }

    public Person getPerson() {
        return person;
    }

    public int getFinishBoxCount() {
        return finishBoxCount;
    }

    public void setFinishBoxCount(int finishBoxCount) {
        this.finishBoxCount = finishBoxCount;
    }

    /**
     * 根据当前位置以及方向，获取下一个格子
     *
     * @param currentX
     * @param currentY
     * @param direction
     * @return
     */
    public BaseGrid findNextGrid(int currentX, int currentY, Direction direction) {
        int i = 0;
        int j = 0;
        l: for (i = 0; i < boardArr.length; i++) {
            for (j = 0; j < boardArr[i].length; j++) {
                BaseGrid oneGrid = boardArr[i][j];
                if (oneGrid == null) {
                    continue;
                }
                if (oneGrid.getX() == currentX && oneGrid.getY() == currentY) {
                    break l;
                }
            }
        }

        if (Direction.UP == direction) {
            i--;
        } else if (Direction.DOWN == direction) {
            i++;
        } else if (Direction.LEFT == direction) {
            j--;
        } else if (Direction.RIGHT == direction) {
            j++;
        }
        if (i < 0 || i > boardArr.length || j < 0 || j > boardArr[i].length) {
            return null;
        }
        return boardArr[i][j];
    }

    public void resetBoard() {
        this.finishBoxCount = 0;
        initBoard();
        boardPanel.repaint();
    }

}
